package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oocl.dao.MerchantApplyRecordDao;
import com.oocl.pojo.MerchantApplyRecord;
import com.oocl.util.DBUtil;

public class MerchantApplyRecordDaoImpl implements MerchantApplyRecordDao{

	@Override
	public int add(MerchantApplyRecord t) {
		String sql="insert into MERCHANT_APPLY_RECORD(ID,M_NAME,IDCARD,HEADING,ADDRESS,STATUS,BAN,BCONTENT,M_ID) values (?,?,?,?,?,?,?,?,?)";
		Connection connection = null;
		PreparedStatement pst=null;
		int m=0;
		try {
			connection = DBUtil.getConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, t.getId());
			pst.setString(2, t.getmName());
			pst.setString(3, t.getIdcard());
			pst.setString(4, t.getHeading());
			pst.setString(5, t.getAddress());
			pst.setInt(6, t.getStatus());
			pst.setBoolean(7, t.isBan());
			pst.setString(8, t.getBcontent());
			pst.setString(9, t.getmId());
			m=pst.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException("MerchantApplyRecordDaoImpl add error");
		} finally{
			DBUtil.release(null, pst, null);
		}
		return m;
	}

	@Override
	public int update(MerchantApplyRecord t) {
		String sql="update MERCHANT_APPLY_RECORD set M_NAME=?,IDCARD=?,HEADING=?,ADDRESS=?,STATUS=?,BAN=?,BCONTENT=?,M_ID=? where ID=?";
		Connection connection = null;
		PreparedStatement pst=null;
		int m=0;
		try {
			connection = DBUtil.getConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, t.getmName());
			pst.setString(2, t.getIdcard());
			pst.setString(3, t.getHeading());
			pst.setString(4, t.getAddress());
			pst.setInt(5, t.getStatus());
			pst.setBoolean(6, t.isBan());
			pst.setString(7, t.getBcontent());
			pst.setString(8, t.getmId());
			pst.setString(9, t.getId());
			m=pst.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException("MerchantApplyRecordDaoImpl update error");
		} finally{
			DBUtil.release(null, pst, null);
		}
		return m;
	}

	@Override
	public int delete(String id) {
		String sql="delete from MERCHANT_APPLY_RECORD where ID=?";
		Connection connection = null;
		PreparedStatement pst=null;
		int m=0;
		try {
			connection = DBUtil.getConnection();
			pst = connection.prepareStatement(sql);
			pst.setString(1, id);
			m=pst.executeUpdate();
		} catch (SQLException e) {
			throw new RuntimeException("MerchantApplyRecordDaoImpl delete error");
		} finally{
			DBUtil.release(null, pst, null);
		}
		return m;
	}

	@Override
	public List<MerchantApplyRecord> findAll() {
		List<MerchantApplyRecord> list = new ArrayList<MerchantApplyRecord>();
		Connection connection = null;
		PreparedStatement pst = null;
		String sql = "select * from MERCHANT_APPLY_RECORD";
		ResultSet rs = null;
		try {
			connection = DBUtil.getConnection();
			pst=connection.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()){
				MerchantApplyRecord merchantApplyRecord = new MerchantApplyRecord();
				merchantApplyRecord.setId(rs.getString("id"));
				merchantApplyRecord.setmName(rs.getString("m_name"));
				merchantApplyRecord.setIdcard(rs.getString("idcard"));
				merchantApplyRecord.setHeading(rs.getString("heading"));
				merchantApplyRecord.setAddress(rs.getString("address"));
				merchantApplyRecord.setStatus(rs.getInt("status"));
				merchantApplyRecord.setBan(rs.getBoolean("ban"));
				merchantApplyRecord.setBcontent(rs.getString("bcontent"));
				merchantApplyRecord.setmId(rs.getString("m_id"));
				list.add(merchantApplyRecord);
			}
			return list;
		} catch (SQLException e) {
			throw new RuntimeException("MerchantApplyRecordDaoImpl findAll error");
		} finally{
			DBUtil.release(rs, pst, null);
		}
	}

	@Override
	public List<MerchantApplyRecord> findByMerchantId(String mid) {
		Connection connection  = null;
		connection = DBUtil.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from MERCHANT_APPLY_RECORD where M_ID=?";
		List<MerchantApplyRecord> list = new ArrayList<MerchantApplyRecord>();
		MerchantApplyRecord merchantApplyRecord = null;
		try {
			ps = connection.prepareStatement(sql);
			ps.setString(1, mid);
			rs = ps.executeQuery();
			while(rs.next()){
				merchantApplyRecord = new MerchantApplyRecord();
				merchantApplyRecord.setId(rs.getString(1));
				merchantApplyRecord.setmName(rs.getString(2));
				merchantApplyRecord.setIdcard(rs.getString(3));
				merchantApplyRecord.setHeading(rs.getString(4));
				merchantApplyRecord.setAddress(rs.getString(5));
				merchantApplyRecord.setStatus(rs.getInt(6));
				merchantApplyRecord.setBan(rs.getBoolean(7));
				merchantApplyRecord.setBcontent(rs.getString(8));
				merchantApplyRecord.setmId(rs.getString(9));
				list.add(merchantApplyRecord);
			}
		} catch (SQLException e) {
			throw new RuntimeException("MerchantApplyRecordDaoImpl findByMerchantId error");
		}finally {
			DBUtil.release(rs, ps, null);
		}
		return list;
	}

}
